package com.lin.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.gson.internal.$Gson$Preconditions;
import com.lin.reggie.entity.ShoppingCart;
import com.lin.reggie.service.ShoppingCartService;
import com.lin.reggie.utils.BaseContext;
import com.lin.reggie.utils.R;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-05-26
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //先设置用户id，指定当前是哪个用户的购物车数据，因为前端没有这个id给我们，
        //用户登录的时候存在了session中
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if (dishId!=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishId);

        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //查询当前菜品是否或者套餐是否在套餐中
        shoppingCart.setNumber(1);
        shoppingCartService.save(shoppingCart);
        one=shoppingCart;
        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){

        //sql:delete from shopping_cart where userId= ?

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }

    @PostMapping("/sub")
    @Transactional
    public R<ShoppingCart> sub (@RequestBody ShoppingCart shoppingCart){

        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        //代表数量减少的商品数量

        if (dishId!=null){
             queryWrapper.eq(ShoppingCart::getDishId,dishId);
             queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            Integer number = shoppingCart1.getNumber();
            if (number>0){
                shoppingCartService.updateById(shoppingCart1);
            }else if (number==0){
                shoppingCartService.removeById(shoppingCart1.getId());
            }else if (number<0){
                return R.error("操作异常");
            }
            return R.success(shoppingCart1);
        }
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper2 = new LambdaQueryWrapper<>();
        if (setmealId!= null){
            queryWrapper2.eq(ShoppingCart::getSetmealId,setmealId);
            queryWrapper2.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            ShoppingCart cart2 = shoppingCartService.getOne(queryWrapper2);
            Integer number2 = cart2.getNumber();
            if (number2>0){
                shoppingCartService.updateById(cart2);
            }else if (number2 == 0){
                shoppingCartService.removeById(cart2.getId());
            }else if (number2<0){
                return R.error("已清空");
            }
            return R.success(cart2);
        }
        return R.error("操作异常，购物车里不存在该商品");
    }
}

